<?php

namespace plugin\admin\app\controller;

use plugin\admin\app\model\RlMember;
use support\Request;
use support\Response;
use plugin\admin\app\model\RlFeedBack;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;
use const support\feed_back_status_1;
use const support\feed_back_status_2;
use const support\feed_back_status_9;

/**
 * 反馈管理 
 */
class RlFeedBackController extends Crud
{
    
    /**
     * @var RlFeedBack
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new RlFeedBack;
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('rl-feed-back/index');
    }

    /**
     * 获取
     * @return Response
     */
    public function select(Request $request): Response
    {
        $page = (int)$request->get('page');
        $limit = (int)$request->get('limit', 10);
        $where['status']=1;
        if ($id=$request->get('id','')){
            $where['_id']=$id;
        }
        $count=RlFeedBack::whereIn('status',[feed_back_status_1,feed_back_status_2])->count();
        $offset=($page-1)*$limit;
        $lists= RlFeedBack::whereIn('status',[feed_back_status_1,feed_back_status_2])->orderBy('id','desc')
            ->offset($offset)->limit($limit)->get()->toArray();
        //获取用户ID
        $memberIds=array_column($lists,'member_id');
        $members=RlMember::whereIn('id',$memberIds)->get()->toArray();
        $members=array_column($members,null,'id');
        //进行昵称头像追加
        foreach ($lists as &$list){
            $list['nickname']=$members[$list['member_id']]['nickname']??'';
            $list['avatar']=$members[$list['member_id']]['avatar']??'';
            $list['mobile']=$members[$list['member_id']]['mobile']??'';
        }
        return json(['code' => 0, 'data' => $lists, 'count'=>$count,'msg' => '成功']);
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            //接收前段信息
            $id=request()->post('id','');
            $replyContent=request()->post('reply_content','');
            RlFeedBack::where('id',$id)->update([
                'reply_content'=>$replyContent,
                'update_time'=>time(),
                'status'=>feed_back_status_2
            ]);
            return $this->json(0,'成功');
        }
        return view('rl-feed-back/update');
    }

}
